package com.example.demo.controller;

import com.example.demo.dao.entity.Department;
import com.example.demo.dao.entity.Doctor;
import com.example.demo.dao.entity.Hospital;
import com.example.demo.service.DepartmentService;
import com.example.demo.service.DoctorService;
import com.example.demo.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    /**
     * 查询医生列表
     * @param model
     * @param doctor
     * @return
     */
    @GetMapping("/list")
    public String getDoctorList(Model model, Doctor doctor) {
        List<Doctor> list = doctorService.lambdaQuery().eq(Doctor::getStatus,0).list();
        model.addAttribute("list", list);
        list.forEach(
                a -> {
                    Department dp = departmentService.getById(a.getDepartmentId());
                    a.setDepartmentName(dp.getName());
                    a.setHospitalName(hospitalService.getById(dp.getHospitalId()).getHospitalName());
                });
        return "doctor/list";
    }

    /**
     * 新增医生页面
     * @param model
     * @param doctor
     * @param id
     * @return
     */
    @GetMapping("addpage")
    public String addpage(Model model, @ModelAttribute Doctor doctor, @RequestParam(value = "id",required = false) Integer id){
        if(!StringUtils.isEmpty(id)){
            Doctor byId = doctorService.getById(id);
            model.addAttribute("doctor",byId);
        }
        List<Hospital> list = hospitalService.list();
        model.addAttribute("hospitals",list);
        return "doctor/add";
    }

    /**
     * 保存更新医生
     * @param model
     * @param doctor
     * @param principal
     * @return
     */
    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    public String saveOrUpdate(Model model, Doctor doctor, Principal principal) {
        String loginName = principal.getName();
        Date now = new Date();
        doctor.setCreatePerson(loginName);
        doctor.setUpdatePerson(loginName);
        if(doctor.getId() == null){
            doctor.setCreateTime(now);
        }
        doctor.setUpdateTime(now);
        doctorService.saveOrUpdate(doctor);
        return getDoctorList(model,doctor);
    }


    /**
     * 删除医生
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public String deleteDepartmentById( @PathVariable Long id) {
        doctorService.lambdaUpdate().eq(Doctor::getId,id).set(Doctor::getStatus,1).update();
        return "success";
    }
}
